package com.xiaomili.halfsugar.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.gc.flashview.FlashView;
import com.google.gson.Gson;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.bean.ArticleBean;
import com.xiaomili.halfsugar.constant.UrlString;
import com.xiaomili.halfsugar.utils.OKHttpUtils;
import com.xiaomili.halfsugar.utils.OKHttpUtils.MyCallback;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ArticleFragment extends Fragment {
    private static final String TAG = "ArticleFragment";
    private Context mContext;

    @InjectView(R.id.flash_view)
    FlashView mFlashView;
    @InjectView(R.id.rg_container)
    RadioGroup mRadioGroup;
    @InjectView(R.id.fl_container)
    FrameLayout mFrameLayout;
    private ArticleBean mArticleBean;
    private List<String> mImgUrls;
    private int mCount;

    public ArticleFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImgUrls = new ArrayList<>();
        mContext = getActivity();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_article, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //加载上面的viewpager
        loadData();


    }

    //数据的下载
    private void loadData() {
        OKHttpUtils.newInstance(mContext).obtainDataAsyncByGet(UrlString.COMMUNITY_ARTICLE_URL, new MyCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                mArticleBean = new Gson().fromJson(result, ArticleBean.class);
                for (int i = 0; i < mArticleBean.data.banner.size(); i++) {
                    mImgUrls.add(mArticleBean.data.banner.get(i).photo);
//                    Log.i(TAG, "url: " + mImgUrls.get(i));
                }
//                Log.i(TAG, "mImgUrls.size: " + mImgUrls.size());
                mFlashView.setImageUris(mImgUrls);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
